package pl.kemot.impl.customer.edit;

import pl.kemot.IPresenterComponentRemove;
import pl.kemot.customer.all.IViewCustomerAll;
import pl.kemot.customer.edit.IPresenterCustomerEdit;
import pl.kemot.customer.edit.IViewCustomerEdit;
import pl.kemot.impl.Context;
import pl.kemot.model.CustomerBO;
import pl.kemot.model.CustomerDTO;

public class PresenterCustomerEditImpl implements IPresenterCustomerEdit {

	private CustomerBO customerBO;
	private IViewCustomerAll viewCustomerAll;
	private IPresenterComponentRemove presenterComponentRemove;
	private IViewCustomerEdit viewCustomerEdit;

	public PresenterCustomerEditImpl() {
	}

	@Override
	public void setCustomerBO(CustomerBO customerBO) {
		this.customerBO = customerBO;
	}

	@Override
	public void setViewCustomerAll(IViewCustomerAll viewCustomerAll) {
		this.viewCustomerAll = viewCustomerAll;

	}

	@Override
	public void setPresenterComponentRemove(IPresenterComponentRemove presenterComponentRemove) {
		this.presenterComponentRemove = presenterComponentRemove;
	}

	@Override
	public void setViewCustomerEdit(IViewCustomerEdit viewCustomerEdit) {
		this.viewCustomerEdit = viewCustomerEdit;
	}

	@Override
	public void perform(Context context) {
		// pobieramy dane z formularza
		CustomerDTO customerDTO = new CustomerDTO();
		customerDTO.setId(viewCustomerEdit.getId());
		customerDTO.setFirstName(viewCustomerEdit.getFirstName());
		customerDTO.setLastName(viewCustomerEdit.getLastName());
		customerDTO.setAge(viewCustomerEdit.getAge());

		// utrwalamy za pomocą utrwalacza
		customerDTO = customerBO.saveOrUpdate(customerDTO);

		// aktualizujemy spis klientów
		viewCustomerAll.addOrUpdateCustomer(customerDTO);

		// usuwamy formularz z UI
		context.put(IPresenterComponentRemove.OBJECT_TO_REMOVE, viewCustomerEdit);
		presenterComponentRemove.perform(context);
	}
}
